package com.kitty.game.task.service.targetHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.config.Skill;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.model.product.entry.TaskStudySkillEntry;
import com.kitty.game.task.model.product.targetparam.StudySkillTargetParam;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.listener.BaseGameEvent;
import com.kitty.listener.event.StudySkillEvent;
import com.kitty.game.utils.Const;
import com.kitty.game.skill.service.SkillService;
import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class StudySkillTargetHandler extends TaskTargetHandler {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void handleEvent(Role role, BaseGameEvent baseGameEvent) {
        /**判断学习的技能是否为已接任务的目标，是则更新目标*/

        /**拿到当前任务目标为需要完成学习技能的*/
        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        List<TaskSet> acceptedTasksMatchTarget = taskService.getAcceptedTasksMatchTarget(role, TaskTargetType.STUDY_SKILL);

        /**没有学习技能的任务目标，返回*/
        if (acceptedTasksMatchTarget.size() == 0) {return ;}

        for (TaskSet taskSet : acceptedTasksMatchTarget) {
            TaskTarget roleDoingTaskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);
            StudySkillTargetParam studySkillTargetParam = (StudySkillTargetParam)roleDoingTaskTarget.getTaskTargetParam();

            StudySkillEvent studySkillEvent = (StudySkillEvent)baseGameEvent;
            int skillId = studySkillEvent.getSkillId();
            short skillLevel = studySkillEvent.getSkillLevel();

            /**判断学习的技能是否匹配任务需求的技能和等级*/
            if (!isMatchSkillAndLevel(role, studySkillTargetParam, skillId, skillLevel)) {continue;}

            taskService.updateTargetProgress(role, taskSet, roleDoingTaskTarget);

            /**一次只能完成一个任务目标*/
            break;
        }
    }

    /**判断是否匹配任务需求的技能和等级*/
    private boolean isMatchSkillAndLevel(Role role, StudySkillTargetParam studySkillTargetParam, int skillId, short skillLevel) {
        /**根据加点选择的是高物伤、高法伤、自定义，和职业获得对应 的skillId列表*/

        Map<String, Byte> nutMap = role.getExtendBox().getAutoAttrib();
        if (nutMap.get("type") == Const.ATTRIB_TYPE_PHYCIAL) {
            /**高物伤 */
            TaskStudySkillEntry skillEntry = studySkillTargetParam.getSkillMap().get(Const.POINT_PHYSICAL);
            logger.trace("匹配高物伤 需要匹配技能：{}，学习技能: {}={}", Json.toJson(skillEntry.getSkillIds()), skillId, skillLevel);
            if (skillEntry != null && skillEntry.getSkillIds() != null) {
                if (skillEntry.getSkillIds().contains(skillId)) {
                    if (skillLevel >= skillEntry.getLevel()) {
                        return true;
                    }
                }
            }
        } else if (nutMap.get("type") == Const.ATTRIB_TYPE_MAGIC) {
            /**高法伤 */
            TaskStudySkillEntry skillEntry = studySkillTargetParam.getSkillMap().get(Const.POINT_MAGIC);
            logger.trace("匹配高法伤 需要匹配技能：{}，学习技能: {}={}", Json.toJson(skillEntry.getSkillIds()), skillId, skillLevel);
            /**法术技能需要匹配职业*/
            List<Integer> skillIds = getSkillIdsMatchJob(role.getPolar(), skillEntry.getSkillIds());
            if (skillIds.contains(skillId)) {
                if (skillLevel >= skillEntry.getLevel()) {
                    return true;
                }
            }
        } else {
            /** 自定义 */
            TaskStudySkillEntry skillEntry = studySkillTargetParam.getSkillMap().get(Const.POINT_SELF);
            logger.trace("匹配自定义 需要匹配技能：{}，学习技能: {}={}", Json.toJson(skillEntry.getSkillIds()), skillId, skillLevel);
            if (skillEntry != null && skillEntry.getSkillIds() != null) {
                if (skillEntry.getSkillIds().contains(skillId)) {
                    if (skillLevel >= skillEntry.getLevel()) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    /**获得传入技能ID列表 匹配对应职业的技能*/
    private List<Integer> getSkillIdsMatchJob(short job, List<Integer> skillIds) {
        List<Integer> matchSkillIds = new ArrayList<Integer>(2);
        SkillService skillService = SpringUtils.getBean(SkillService.class);
        for (int skillId : skillIds) {
            Skill skill = skillService.getSkillInfo(skillId);
            if (skill == null) {continue;}

            /**判断职业是否相等*/
            if (skill.getSkillJob() != job) {continue;}

            matchSkillIds.add(skill.getSkillId());
        }
        return matchSkillIds;

    }

    @Override
    public void triggerNow(Role role, TaskSet taskSet, TaskTarget taskTarget) {

    }
}
